<?php
class My_API_sm05_Users
{
    private $_config;

    public function __construct($gameId = null)
    {
        if(!isset($gameId) || !file_exists(CONFIG_PATH2 . "/$gameId.ini")){
            return false;
        }
        
        $config = new Zend_Config_Ini(CONFIG_PATH2 . "/$gameId.ini",APPLICATION_ENV);
        $this->_config = $config;
    }
    
     public function getUsernameByRolename($listRoleName, $serverId){
        $i = 0;
		$infoUsername = array();
        $serverIdPartner = str_replace('S','',$serverId);
        $serverIdPartner='17100'.$serverIdPartner;
		$userCheck = array_chunk($listRoleName, 50);
		foreach ($userCheck as $listCheck){
            $accounts = implode(',',$listCheck);
            //pr($accounts)
            $urlAPI = $this->_config->getUsernameInfo->url . "?roleNames=$accounts&server_id={$serverIdPartner}";
            $result = file_get_contents($urlAPI);
            $result = json_decode($result);
            if(isset($result->status) && $result->status == 1){
                foreach($result->result as $row){
              
                        //pr($r->level);
                        if($row->username){
        					$infoUsername[$i]['serverId'] = $serverId;
        					$infoUsername[$i]['username'] = $row->union_name;
        					//$infoUsername[$i]['roleLevel'] = $r->level;
                           if(isset($this->_config->statistics->role))
                            {
                                $title=$this->_config->statistics->role->toArray();
                                foreach($title as $keyTitle => $valTitle)
                                {
                                    if(isset($row->$keyTitle))
                                    {
                                        $infoUsername[$i][$keyTitle] = $row->$keyTitle;
                                    }
                                }
                            }
                        }
                        else{
                            continue;
                        }
    					$i++;
                    
                }
			}else{
				continue;
			}
		}
        
		return $infoUsername;
    }
    
    public function getUsersByServerId($listUser, $serverId, $channel = ''){
        $i = 0;
		$infoUsers = array();
        
		$userCheck = array_chunk($listUser, 50);
		foreach ($userCheck as $listCheck){
            $time = time();
            //achapi3005,chanmayghe,viptitmit,tuan160788,sadangel1510,dieukhung,hqtvela
            //achapi3005,chanmayghe,viptitmit,tuan160788,sadangel1510
            $accounts = implode(',',$listCheck);
            $sign = md5($time.md5($this->_config->getUserInfo->agent.md5($accounts)));
            $postData   = array(
                'time'      => $time,
                'sign'      => $sign,
                "accounts"  =>$accounts
            );
            $urlAPI = "{$this->_config->getUserInfo->url}?accounts={$accounts}&time={$time}&sign={$sign}";
         
            //echo $urlAPI."<br />";//exit;
            //$postData = json_encode($postData);exit;
            //$curl = curl_init();
//            
//            curl_setopt($curl, CURLOPT_URL, $this->_config->getUserInfo->url);
//            #curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
//            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
//            curl_setopt($curl, CURLOPT_POST, 1);
//            curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
//            $result = curl_exec($curl);    
//            curl_close($curl);
            $result = file_get_contents($urlAPI);   
            $result = json_decode(utf8_encode($result));
           
            if(isset($result->status) && $result->status == 1){
                foreach($result->result as $key=>$row){
                    if(!isset($row[0])){
                        continue;
                    }
                        
					$infoUsers[$i]['serverId'] = $serverId;
					$infoUsers[$i]['roleName'] = $row[0]->roleName;
					//$infoUsers[$i]['roleLevel'] = $row[0]->level;
                     if(isset($this->_config->statistics->role))
                        {
                            $title=$this->_config->statistics->role->toArray();
                            foreach($title as $keyTitle => $valTitle)
                            {
                                if(isset($row->$keyTitle))
                                {
                                    $infoUsers[$i][$keyTitle] = $row[0]->$keyTitle;
                                }
                            }
                        }
					$i++;
                }
			}else{
				continue;
			}
		}
        
		return $infoUsers;
    }
}